    //
//  LCTimingClosureViewController.m
//  JKFuYinProject
//
//  Created by admin on 2018/7/7.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCTimingClosureViewController.h"

#define TimingClosureCellIdentifier @"TimingClosureCellIdentifier"

@interface LCTimingClosureViewController ()
<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic,strong) NSArray *array;//
@property (nonatomic,strong) UILabel *titleLabelView;

@end

@implementation LCTimingClosureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];

    self.title = @"定时关闭";
    [self.view addSubview:self.tableView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
        }
    }];
    if ([self.vcInitDictData[@"back"] isEqualToString:@"dismiss"]) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)];
    }
}
- (void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)loadData{
    self.array = @[
                   @"不开启",
                   @"10分钟后",
                   @"20分钟后",
                   @"30分钟后",
                   @"60分钟后",
                   @"90分钟后"
                   ];
}
//tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TimingClosureCellIdentifier];
    if (self.array) {
        cell.textLabel.text =
        self.array[indexPath.row];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    imageView.image = [UIImage imageNamed:@"定时关闭对号"];
    cell.accessoryView = imageView;
    [imageView sizeToFit];
    if (_UserM.selectedRow == indexPath.row) {
        cell.accessoryView.hidden = NO;
    }else{
        cell.accessoryView.hidden = YES;
    }
    imageView.width = imageView.width / 2;
    imageView.height = imageView.height / 2;
    
    if (_UserM.selectedRow == 0 && _UserM.timingCloseingTimeNum <= 0) {
        _titleLabelView.text = @"计时结束后, 将暂停播放歌曲";
    }else {
        __weak typeof(self) weakSelf = self;
        _titleLabelView.text =
        [NSString stringWithFormat:@"%@秒后%@",[weakSelf getShowDateNumWithNum:_UserM.timingCloseingTimeNum],@", 将暂停播放歌曲"];
        _UserM.timingCloseingTimeBlock = ^(NSInteger timingCloseingTimeNum) {
            NSLog(@"timingCloseingTimeNum = %@", [weakSelf getShowDateNumWithNum:timingCloseingTimeNum]);
            weakSelf.titleLabelView.text =
            [NSString stringWithFormat:@"%@后%@",[weakSelf getShowDateNumWithNum:timingCloseingTimeNum],@", 将暂停播放歌曲"];
            if (timingCloseingTimeNum <= 0) {
                weakSelf.titleLabelView.text = @"计时结束后, 将暂停播放歌曲";
            }
            if (timingCloseingTimeNum < 0) {
                [weakSelf.tableView reloadData];
            }
        };
    }
    return cell;
}

- (NSString *)getShowDateNumWithNum:(NSInteger)num {
    NSInteger fen = num/60;
    NSInteger miao = num%60;
    NSString *timeStr = [NSString new];
    timeStr = [NSString stringWithFormat:@"%02zd分%02zd秒", fen,miao];
    return timeStr;
}

//height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
//numberOfRows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
//didSelectRow
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _UserM.selectedRow = indexPath.row;
    [self setupTimingCloseingTime];
    [tableView reloadData];
}
- (void)setupTimingCloseingTime
{
    if (_UserM.selectedRow >= 0 && _UserM.selectedRow < 4) {
        _UserM.timingCloseingTimeNum = _UserM.selectedRow * 10 * 60;
    }
    if (_UserM.selectedRow == 4) {
        _UserM.timingCloseingTimeNum = 60 * 60;
    }
    if (_UserM.selectedRow == 5) {
        _UserM.timingCloseingTimeNum = 90 * 60;
    }
    
    if (_UserM.selectedRow > 0) {
        [_UserM setupTimingCloseingTime];
    }
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TimingClosureCellIdentifier];
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.tabBarController.tabBar.translucent = NO;

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0,
                                                                0.0,
                                                                ScreenWidth,
                                                                40)];

        UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, 20)];
        _titleLabelView = titleLabelView;
        _titleLabelView.textAlignment = NSTextAlignmentCenter;
        _titleLabelView.textColor = lightColor;
        _titleLabelView.font = FONT_(13);
        _titleLabelView.text =
        [NSString stringWithFormat:@"%@%@",@"计时结束后",@", 将暂停播放歌曲"];
        [view addSubview:_titleLabelView];
        _tableView.tableHeaderView = view;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
